package com.alili_tv.system.controller;

import com.alili_tv.system.common.BaseContext;
import com.alili_tv.system.common.LoginRequired;
import com.alili_tv.system.common.R;
import com.alili_tv.system.entity.Follow;
import com.alili_tv.system.service.FollowService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户关注表 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2023-03-17
 */
@RestController
@RequestMapping(value = "/follow", produces = "application/json")
public class FollowController {

    @Resource
    private FollowService followService;

    /**
     * 关注或取消关注uploader
     *
     * @param uploaderId
     * @param isFollow   true 关注， false 取消关注
     * @return
     */
    @LoginRequired
    @PostMapping("/{uploaderId}/{isFollow}")
    public R<String> follow(@PathVariable Long uploaderId, @PathVariable Boolean isFollow) {
        if (BaseContext.getCurrentId().equals(uploaderId)) {
            return R.error("不能关注自己~~");
        }

        if (isFollow) {
            followService.save(new Follow(BaseContext.getCurrentId(), uploaderId));
        } else {
            LambdaQueryWrapper<Follow> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper
                    .eq(Follow::getCreateUser, BaseContext.getCurrentId())
                    .eq(Follow::getUploader, uploaderId);
            followService.remove(queryWrapper);
        }
        return R.success("success");
    }
}
